package com.guo.hui.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.guo.hui.model.ponal.User;
import com.guo.hui.service.UserService;
import com.guo.hui.util.UtilJson;
import com.guo.hui.util.UtilMail;

@Controller
@RequestMapping(value = "/ponal")
public class CommonController {

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/login")
	public String login() {
		return "common/login";
	}

	@ResponseBody
	@RequestMapping(value = "/vaildata")
	public String vaildata(HttpServletResponse response, HttpServletRequest request, @RequestParam String username,
			@RequestParam String password, String flag) {
		// shiro 权限验证
		// ShiroToken token = new ShiroToken(username, password);
		// Subject subject = SecurityUtils.getSubject();
		// subject.login(token);
		// Session session = subject.getSession();
		// User user = (User) subject.getPrincipal();
		// session.setAttribute("user", user);
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname("管理员");
		if ("1".equals(flag)) {
			setCookie(user, response, request);
		}
		return "1";
	}

	void setCookie(User user, HttpServletResponse response, HttpServletRequest request) {
		// 创建Cookie
		Cookie nameCookie = new Cookie("cmsname", user.getUsername());
		Cookie pswCookie = new Cookie("cmspsss", user.getPassword());
		// 设置Cookie的父路径
		nameCookie.setPath(request.getContextPath() + "/");
		pswCookie.setPath(request.getContextPath() + "/");
		// 设置缓存日期
		nameCookie.setMaxAge(7 * 24 * 60 * 60);
		pswCookie.setMaxAge(7 * 24 * 60 * 60);
		// 加入Cookie到响应头
		response.addCookie(nameCookie);
		response.addCookie(pswCookie);
	}

	@RequestMapping(value = "/logout")
	public String logout() {
		// 清除session,也就是清除会话,重新登录或返回首页浏览信息
		return "common/login";
	}

	@RequestMapping(value = "/register")
	public String register(HttpServletResponse response, HttpServletRequest request) {
		return "common/register";
	}

	@ResponseBody
	@RequestMapping(value = "/registerdata")
	public String registerdata(HttpServletRequest request, @RequestParam String username, @RequestParam String password,
			@RequestParam String email) {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setEmail(email);
		String userjson = UtilJson.toJson(user);
		String weburl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
				+ request.getContextPath();// 项目根路径
		weburl = weburl + "/ponal/active?userjson=" + userjson;
		try {
			UtilMail.sendMail(username, email, weburl);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return "1";
	}

	@ResponseBody
	@RequestMapping(value = "/active", produces = "text/html;charset=UTF-8")
	public String active(String userjson) throws UnsupportedEncodingException {
		String msg = "网络异常,注册失败,请重新注册!";
		String ss = new String(userjson.getBytes("ISO-8859-1"), "utf-8");
		if (!StringUtils.isEmpty(userjson)) {
			User user = (User) UtilJson.fromJson(ss, User.class);
			userService.saveUser(user);
			msg = "恭喜您!" + "注册成功!";
		}
		return msg;
	}

}
